package com.test.nnooolwdeaer.util_wooden.address;

import android.text.TextUtils;

import com.test.nnooolwdeaer.constant_wooden.Constant_wooden;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Objects;
import java.util.Set;

public class AddressUtil_Viet_wooden {

    private static class Inner_wooden {
        private static final AddressUtil_Viet_wooden instance_wooden = new AddressUtil_Viet_wooden();
    }

    private AddressUtil_Viet_wooden(){}

    public static AddressUtil_Viet_wooden getInstance_wooden(){
        return Inner_wooden.instance_wooden;
    }

    private final HashMap<String,ArrayList<String>> states_wooden = new HashMap<>();

    public void parse_wooden(JSONArray statesJA){
        try {
            for (int i=0;i<statesJA.length();i++){
                JSONObject statesJO = statesJA.getJSONObject(i);
                JSONArray citiesJA = statesJO.optJSONArray("children");
                ArrayList<String> cities = new ArrayList<>();
                for (int j = 0; j< Objects.requireNonNull(citiesJA).length(); j++){
                    JSONObject citiesJO = citiesJA.getJSONObject(j);

                    cities.add(citiesJO.optString("name"));
                }
                states_wooden.put(statesJO.optString("name"),cities);
            }
        }catch (Exception e){
            if (Constant_wooden.Debug) e.printStackTrace();
        }
    }

    public String[] getStates_wooden(){
        Set<String> statesSet = states_wooden.keySet();
        return statesSet.toArray(new String[statesSet.size()]);
    }

    public String[] getCities_wooden(String state){
        if (TextUtils.isEmpty(state)) return new String[0];
        ArrayList<String> cities = Objects.requireNonNull(states_wooden.get(state));
        return cities.toArray(new String[cities.size()]);
    }

}
